package de.breitbeil.jbooking.ui.form;

import java.util.HashMap;

import javax.swing.JComponent;
import javax.swing.JLabel;

import org.springframework.richclient.filechooser.DefaultFileFilter;
import org.springframework.richclient.form.AbstractForm;
import org.springframework.richclient.form.binding.swing.SwingBindingFactory;
import org.springframework.richclient.form.builder.TableFormBuilder;

import de.breitbeil.jbooking.richclient.filechooser.FileChooserComboBox;
import de.breitbeil.jbooking.richclient.form.binding.ComboBoxDaoBinder;
import de.breitbeil.jbooking.util.NullAwarePropertyComparator;
import de.breitbeil.jbooking.util.PersonFilter;

public class BookingTemplateForm extends AbstractForm {

    public static final String ID = "bookingTemplateForm";
    
    private boolean withBooking;
    
    public BookingTemplateForm(BookingTemplateFormObject formObject) {
        this(formObject, true);
    }
    
    public BookingTemplateForm(BookingTemplateFormObject formObject, boolean withBooking) {
        super(formObject);
        setId(ID);
        this.withBooking = withBooking;
    }
    
    @Override
    protected JComponent createFormControl() {
        SwingBindingFactory bindingFactory = (SwingBindingFactory) getBindingFactory();
        TableFormBuilder formBuilder = new TableFormBuilder(bindingFactory);
        formBuilder.setLabelAttributes("colGrId=label colSpec=right:pref");

        DefaultFileFilter filter = new DefaultFileFilter();
        filter.addExtension("rtf");
        filter.setDescription("Rich Text Format");
        
        FileChooserComboBox templateChooser = new FileChooserComboBox(getFormModel(), "template");
        FileChooserComboBox outputChooser = new FileChooserComboBox(getFormModel(), "output");
        
        templateChooser.setFilter(filter);
        outputChooser.setFilter(filter);

        formBuilder.addSeparator(getMessage(ID+".fileSeparator.label"));
        formBuilder.row();
        formBuilder.getLayoutBuilder().cell( new JLabel("Template"), formBuilder.getLabelAttributes() );
        formBuilder.getLayoutBuilder().gapCol();        
        formBuilder.getLayoutBuilder().cell( templateChooser.getControl() );
        formBuilder.row();
        formBuilder.getLayoutBuilder().cell( new JLabel("Zieldatei"), formBuilder.getLabelAttributes() );
        formBuilder.getLayoutBuilder().gapCol();        
        formBuilder.getLayoutBuilder().cell( outputChooser.getControl() );
        formBuilder.row();
        formBuilder.add("openOutput");
        formBuilder.row();
        
        if(withBooking) {
            formBuilder.addSeparator(getMessage(getId()+".bookingSeparator.label"));
            formBuilder.row();
            formBuilder.add("person");
            formBuilder.row();
            formBuilder.addBinding(bindingFactory.createBinding("booking", createBookingContext()), "", "");
        }
        
        return formBuilder.getForm();
    }
    
    protected HashMap<String, Object> createBookingContext() {
        HashMap<String, Object> bookingContext = new HashMap<String, Object>();
        PersonFilter personFilter = new PersonFilter(getFormModel().getValueModel("person"));
        
        getFormModel().getValueModel("person").addValueChangeListener(personFilter);
        
        bookingContext.put(ComboBoxDaoBinder.FILTER_KEY, personFilter);
        bookingContext.put(ComboBoxDaoBinder.COMPARATOR_KEY, new NullAwarePropertyComparator("from", false, false));
        
        return bookingContext;
    }
}
